/*
 * scumix.swat.spark.DatePicker
 * 日期选择器
 *
 * author: scumyang
 * create: 2009-09-13
 * update: 2009-12-25
 *			2010-01-27: *浏览器兼容性修正
 * notes : 需导入 scumix.swat.swat3.js
 */

/*
用法:
	
1.声明变量:
var t = null;

2.初始化:
$.pageReady(function()
{
	t = new scumix.swat.spark.DatePicker();
});

3.显示:
show( 绑定对象, 是否包含时间 )
如: <input type="text" id="edDate" onmousedown="DP.show(this,0)"/>

*/

scumix.swat.spark.DatePicker = $.newClass();
scumix.swat.spark.DatePicker.prototype =
{
	constructor: function()
	{
		this.id = $.makeGlobalId( 'SxDatePicker' );
		this.ElName = this.id;

		this.initialized = false;
		this.yearBegin = 1900;
		this.yearEnd = 2100;
		this.value = '';
		this.closeAfterPicked = true;

		this.isVisible = false;

		this.EFrame = null;
		this.EDateCell = null;

		this.onPick = null;

		this.setDate( new Date());
		this.initView();
	},

	initView: function()
	{
		// main container
		var oFrame = $.createElement({type:'div', id:this.ElName, css:'zIndex:9', attr:{className:'SxDatePicker',exDragable:1,exTopOnDrag:1}});
		
		// title-bar
		var oTitle = $.createElement({type:'div', id:this.ElName+'_Title', css:'', attr:{className:'SxDatePicker_Title',exPass:1}});
			var o = $.createElement({type:'div', id:this.ElName+'_TitleText', css:'', attr:{className:'SxDatePicker_Title_Text',innerHTML:"选择日期",exPass:1}});
		oTitle.appendChild( o );

			o = document.createElement('DIV');
			o.className = 'SxSpark_SysBtn_Close';
			o.title = '关闭';
			o.onclick = this.hide.bind(this);
			o.onmouseover = function(){this.className = 'SxSpark_SysBtn_Close_h'}
			o.onmouseout = function(){this.className = 'SxSpark_SysBtn_Close'}
		oTitle.appendChild( o );
			o = document.createElement('DIV');
			o.className = 'SxSpark_SysBtn_Info';
			o.title = '关于';
			o.onclick = this.DoAbout;
			o.onmouseover = function(){this.className = 'SxSpark_SysBtn_Info_h'}
			o.onmouseout = function(){this.className = 'SxSpark_SysBtn_Info'}
		oTitle.appendChild( o );

		// qselect bar
		var oQSelectBar = $.createElement({type:'div',id:this.id+'_QSelectBar', css:'', attr:{className:'SxDatePicker_QSelect',exPass:1}});

			// today
			o = $.createElement({type:'div',id:this.id+'_QS_Today',css:'',attr:{className:'SxDatePicker_QSelect_Today',innerHTML:'今天'}});
			o.onmouseover = function(){ this.className = 'SxDatePicker_QSelect_Today_h' }
			o.onmouseout = function(){ this.className = 'SxDatePicker_QSelect_Today' }
			o.onclick = this.DoTodayClick.bind(this);
			oQSelectBar.appendChild( o );

			// year list
			o = $.createElement({type:'select',id:this.id+'_QS_Year',css:'',attr:{className:'SxDatePicker_QSelect_Year'}});
			o.onchange = this.DoYearChange.bind(this);
			for( var i=this.yearBegin; i<=this.yearEnd; i++ )
			{
				var opt = document.createElement( 'option' );
				opt.value = i;
				opt.innerHTML = i;
				o.appendChild( opt );
			}
			this.EQSelectYear = o;
			oQSelectBar.appendChild( o );

			// year StepButton
			o = new scumix.swat.spark.StepButton( oQSelectBar, {min:this.yearBegin,max:this.yearEnd,value:this.year,step:1,bind:this.EQSelectYear,repeatDelay:500,repeatRate:300});

			// month list
			o = $.createElement({type:'select',id:this.id+'_QS_Month',css:'',attr:{className:'SxDatePicker_QSelect_Month'}});
			o.onchange = this.DoMonthChange.bind(this);
			for( var i=0; i<12; i++ )
			{
				var opt = document.createElement( 'option' );
				opt.value = i;
				opt.innerHTML = ( i + 1 ) + '月';
				o.appendChild( opt );
			}
			this.EQSelectMonth = o;
			oQSelectBar.appendChild( o );

			// month StepButton
			o = new scumix.swat.spark.StepButton( oQSelectBar, {min:0,max:11,value:this.month,step:1,bind:this.EQSelectMonth,repeatDelay:500,repeatRate:500});

		oFrame.appendChild( oTitle );
		oFrame.appendChild( oQSelectBar );

		this.EFrame = oFrame;
		this.drawDateCell();

		document.body.appendChild( oFrame );
		$(document).addEvent( 'click', this.DoClickOut.bind(this));

		this.EQSelectYear.value = this.year;
	},


	drawDateCell: function()
	{
		function _isDate( n )
		{
			return ( n >= 1 && n <= lDate ) ? n : ' ';
		}

		if( this.EDateCell ) this.EFrame.removeChild( this.EDateCell );

		var dayName = ['日','一','二','三','四','五','六']
		var oDateCell = $.createElement({type:'table',id:this.id+'_DateCell',css:'',attr:{className:'SxDatePicker_DateCell',cellPadding:0,cellSpacing:0}});
		var tb = document.createElement('tbody');

		// 星期标题
		var tr = document.createElement('tr');
		tr.className = 'SxDatePicker_DateCell_Header';
		for( var i=0; i<7; i++ )
		{
			var td = document.createElement('td');
			td.innerHTML = dayName[i];
			tr.appendChild( td );
		}
		tb.appendChild( tr );
		oDateCell.appendChild( tb );

		// 日期
		var D = new Date( this.year, this.month, 1 ).getDay();
		var fDate = 1 - D;
		var lDay = new Date( this.year, this.month + 1, 0 ).getDay();
		var lDate = new Date( this.year, this.month + 1, 0 ).getDate();

		D = new Date();
		var curYear = D.getYear() + ( $.browser.msie ? 0 : 1900 );
		var curMonth = D.getMonth();
		var curDate = D.getDate() + 1;

		for( var i=1, j=fDate; i<7; i++ )
		{
			tr = document.createElement('tr');
			for( var k=0; k<7; k++ )
			{
				td = document.createElement( 'td' );
				td.className = 'item';
				td.innerHTML = _isDate( j++ );

				// 周末用不同颜色
				if( k == 0 ) td.style.color = '#f60';
				else if( k == 6 ) td.style.color = '#f60';

				// 今天用不同颜色
				if( this.year == curYear && this.month == curMonth && j == curDate )
				{
					td.style.color = '#fff';
					td.style.border = '1px solid #03a';
					td.style.backgroundColor = '#06e';
				}

				td.onmouseover = function(){ if( this.innerHTML != ' ' ) this.className='itemHot' }
				td.onmouseout = function(){ if( this.innerHTML != ' ' ) this.className='item' }
				td.onclick = this.DoPickDate.bind(this);
				tr.appendChild( td );
			}
			tb.appendChild( tr );
		}

		oDateCell.appendChild( tb );
		this.EDateCell = oDateCell;
		this.EFrame.appendChild( oDateCell );
	},

	// Set internal date value
	// D: JS Date object
	setDate: function( D )
	{
		this.year = D.getYear() + ( $.browser.msie ? 0 : 1900);
		this.month = D.getMonth();
		this.date = D.getDate();
		this.hour = D.getHours();
		this.minute = D.getMinutes();
		this.second = D.getSeconds();
		this.msecond = D.getMilliseconds();
	},

	updateTime: function()
	{
		var now = new Date();
		this.hour = now.getHours();
		this.minute = now.getMinutes();
		this.second = now.getSeconds();
		this.msecond = now.getMilliseconds();
	},

	buildDateStr: function( y, m, d )
	{
		if( m < 10 ) m = '0' + m;
		if( d < 10 ) d = '0' + d;
		var S = y + '-' + m + '-' + d;

		if( this.withTime )
		{
			this.updateTime();
			var hh = this.hour < 10 ? '0' + this.hour : this.hour;
			var mm = this.minute < 10 ? '0' + this.minute : this.minute;
			var ss = this.second < 10 ? '0' + this.second : this.second;
			S += ' ' + hh + ':' + mm + ':' + ss;
		}

		return S;
	},

	DoAbout: function( evt )
	{
		alert( "组件：日期选择器 (cn.scumix.swat.spark.DatePicker)\t\n版本：3.0.0.1\n更新：2009-12-25\n\n\n反馈：scumyang@163.com\n本组件基于 SCUMiX SWAT 3.0 (Web Edition)" );
	},

	// hide me
	hide: function()
	{
		$(this.ElName).hide();
		this.isVisible = false;
	},

	show: function( bindObj, withTime )
	{
		var e = $E(bindObj);
		var p = $(bindObj).absPos();
		$(this.EFrame).moveTo( p.left, p.top + e.offsetHeight + 1 ).show();
		this.EBind = e;
		this.withTime = withTime || false;
		this.isVisible = true;
	},

	DoTodayClick: function( E )
	{
		this.setDate( new Date());
		this.drawDateCell();
		this.EQSelectYear.value = this.year;
		this.EQSelectMonth.value = this.month;
	},

	DoYearChange: function( E )
	{
		this.year = this.EQSelectYear.value;
		this.drawDateCell();
	},

	DoMonthChange: function( E )
	{
		this.month = this.EQSelectMonth.value;
		this.drawDateCell();
	},

	DoPickDate: function( E )
	{
		var e = $.getTarget( E );
		if( e.innerHTML && e.innerHTML != ' ' )
		{
			this.value = this.buildDateStr( this.year, parseInt( this.month ) + 1, e.innerHTML );
			if( this.EBind ) this.EBind.value = this.value;
			if( this.closeAfterPicked ) this.hide();
		}
	},

	DoClickOut: function( E )
	{
		if( !this.isVisible ) return;
		var e = $.getTarget(E);
		if( !$(this.EFrame).ptIn( E ) && $.getTarget(E) != this.EBind ) this.hide();
	}
}
